﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Shapes;
using System.Text;
using Notas.Model;
using System.Windows.Media;

namespace Notas.View
{
    public partial class NotePage : PhoneApplicationPage
    {

        private Notas.Model.Nota nota;
        GestorBd gestor = new GestorBd();
          
        public NotePage()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            nota = new Model.Nota();
            if (NavigationContext.QueryString["Action"].Equals("Modificar"))
            {
                int id = Int32.Parse( NavigationContext.QueryString["Id"]);
                nota = gestor.getDb().Notas.Where(N => N.Id==id).First();
                PageName.Text = "Modificar Nota";
                selectRect();
            }
            else {
                nota.Color = Colors.Red.ToString();
                gestor.getDb().Notas.InsertOnSubmit(nota);
            }

            ContentPanel.DataContext = nota;
        }

        private void UnselectRect()
        {
            foreach (var rect in colorSelector.Children)
            {
                updateRectSize(rect, 50);
            }
        }

        private static void updateRectSize(UIElement rect, int size)
        {
            ((Rectangle)rect).Width = size;
            ((Rectangle)rect).Height = size;
        }

        private void selectRect() 
        {
            UnselectRect();
            foreach (var rect in colorSelector.Children)
            {
                var color = (System.Windows.Media.SolidColorBrush)((Rectangle)rect).Fill;
       
                if (color.Color.ToString().Equals(nota.Color))
                {
                    updateRectSize(rect, 80);
                    ContenidoTxt.BorderBrush = color;
                    nota.Color = color.Color.ToString();
                    break;
                }
            }
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);
           
            if (nota.Titulo != "")
            {
                gestor.getDb().SubmitChanges();
           
            }
            else {
                
           }


        }

        private void Rectangle_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            var color = (System.Windows.Media.SolidColorBrush)((Rectangle)(sender)).Fill;
            nota.Color = color.Color.ToString();
            selectRect();
        }

        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            gestor.getDb().Notas.DeleteOnSubmit(nota);
            gestor.getDb().SubmitChanges();
            NavigationService.Navigate(new Uri("/View/MainPage.xaml", UriKind.Relative));

        }

    }
}